﻿using BCL.WPFComponent.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Devices;
namespace DevicesWPF
{
    /// <summary>
    /// UCDevicesCollectionConfig.xaml 的交互逻辑
    /// </summary>
    public partial class UCDevicesCollectionConfig : BaseFunctionControl
    {
        public UCDevicesCollectionConfig()
        {
            InitializeComponent();
            List<PropertyNodeItem> tree = new List<PropertyNodeItem>();
            DevicesCollection.GetCanAddDevices().ForEach(d =>
            {
                PropertyNodeItem node = tree.FirstOrDefault(n => n.Name == d.Brand);
                if (node == null)
                    tree.Add(node = new PropertyNodeItem { IsEnabled = true, Name = d.Brand, Children = new List<PropertyNodeItem>() });
                node.Children.Add(new PropertyNodeItem { Tag=d, IsEnabled = d.Num > 0, IsAdd = d.Num > 0 ? Visibility.Visible : Visibility.Collapsed, Name = d.Name, Value = d, Icon = "image/2.png" });
            });
            this.treeView.ItemsSource = tree;

            DevicesCollection.DevicesCountChanged += DevicesCollection_DevicesCountChanged;
            DevicesCollection_DevicesCountChanged();
        }

        private void DevicesCollection_DevicesCountChanged()
        {
            MainPanel.Children.Clear();
            if (DevicesCollection.Devices == null)
                return;

            foreach (var item in DevicesCollection.Devices)
            {
                UCDevicesState us = new UCDevicesState(item);
                us.Margin = new Thickness(10, 10, 5, 5);
                MainPanel.Children.Add(us);
            }
        }

        public override void LanguageBinding()
        {

        }
        public override void ReloadData(object data)
        {
            base.ReloadData(data);
        }

        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            PropertyNodeItem item = treeView.SelectedItem as PropertyNodeItem;
            if (item == null || item.Tag == null)
                return;
            DevicesInformation info = item.Tag as DevicesInformation;

            if (info == null)
                return;
            IDevices newDev = Devices.DevicesCollection.CreateDevices(null, info.ClassInfo);

            UCDevicesConfig uCDevicesConfig = new UCDevicesConfig(newDev);
            uCDevicesConfig.ShowDialog(null);
            if (uCDevicesConfig.MessageBoxResult== MessageBoxResult.OK)
                DevicesCollection.Add(newDev);
        }
    }


    public class PropertyNodeItem
    {
        public string Icon { get; set; }
        public string EditIcon { get; set; }
        public string DisplayName { get; set; }
        public string Name { get; set; }
        public bool IsEnabled { get; set; }
        public Visibility IsAdd { get; set; }
        public DevicesInformation Value { get; set; }
        public List<PropertyNodeItem> Children { get; set; }
        public object Tag { get; set; }
        public PropertyNodeItem()
        {
            Icon = "image/1.png";
            IsAdd = Visibility.Collapsed;
        }

    }
}
